﻿// You are given a sequence of positive integer values written into a string, separated by spaces. 
// Write a function that reads these values from given string and calculates their sum. 
// Example: string = "43 68 9 23 318" -> result = 461

using System;

public class CalcSumOfStringSequence
{
    private static readonly char separator = ' ';

    private static void Main(string[] args)
    {
        string stringSequence = ConsoleInputString();
        int sum = GetSumOfStringSequence(stringSequence, separator);

        Console.WriteLine("Sum of the numbers sequence is: {0}", sum);
    }

    private static string ConsoleInputString()
    {
        string consoleInputString;
        do
        {
            Console.Write("Enter number sequence separated by interval: ");
            consoleInputString = Console.ReadLine();
        }
        while (!IsNumberSequence(consoleInputString));

        return consoleInputString;
    }

    private static bool IsNumberSequence(string consoleInputString)
    {
        foreach (char c in consoleInputString)
        {
            if (!c.Equals(separator) && !Char.IsDigit(c))
            {
                return false;
            }
        }

        return true;
    }

    private static int GetSumOfStringSequence(string stringSequence, char separator)
    {
        string[] numbers = stringSequence.Split(new char[] { separator }, StringSplitOptions.RemoveEmptyEntries);
        int sum = 0;
        foreach (string num in numbers)
        {
            sum += int.Parse(num);
        }

        return sum;
    }
}